/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Modele;

import Bdd.DivorceRepository;
import Metier.*;
import java.util.Date;
import java.util.ArrayList;

/**
 *
 * @author Babs
 */
public class ModeleDivorce extends javax.swing.table.AbstractTableModel implements Modele {

    /* Attributs privée */
    private String[] nomsColonnes = {"Index", "Name 1", "Surname 1", "Name 2", "Surname 2", "Date"};
    private ArrayList<Divorce> donnees;
    private DivorceRepository divorceRepo;
    
    /* Constructeurs */
    public ModeleDivorce() {
        donnees = new ArrayList<Divorce>();
        divorceRepo = new DivorceRepository();
    }

    /* Methodes publiques */
    public int getColumnCount() {
        return nomsColonnes.length;
    }
    public int getRowCount() {
        return donnees.size();
    }
    public String getColumnName(int col) {
        return col >= 0 ? nomsColonnes[col] : null;
    }
    public Object getValueAt(int row, int col) {
        switch(col) {
            case 0:
                return donnees.get(row).getId();
            case 1:
                return donnees.get(row).getVip1().getNom();
            case 2:
                return donnees.get(row).getVip1().getPrenom1();
            case 3:
                return donnees.get(row).getVip2().getNom();
            case 4:
                return donnees.get(row).getVip2().getPrenom1();
            default:
                return donnees.get(row).getDate();
        }
    }
    @Override
    public Class getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    }
    @Override
    public boolean isCellEditable(int row, int col) {
        return false;
    }
    public void setValueAt(Object value, int row, int col) {
        switch(col) {
            case 0:
                donnees.get(row).setId((Integer)value);
                break;
            case 1:
                donnees.get(row).getVip1().setNom((String)value);
                break;
            case 2:
                donnees.get(row).getVip1().setPrenom1((String)value);
                break;
            case 3:
                donnees.get(row).getVip2().setNom((String)value);
                break;
            case 4:
                donnees.get(row).getVip2().setPrenom1((String)value);
                break;
            default:
                donnees.get(row).setDate((java.sql.Date)value);
                break;
        }
        fireTableCellUpdated(row, col);
    }
    public void showData(ArrayList list) {
        for(Divorce d : (ArrayList<Divorce>) list)
            ajoutLigne(d);
    }
    public void ajoutLigne(Object d) {
        donnees.add((Divorce)d);
        int ligne = donnees.size() - 1;
        fireTableChanged(new javax.swing.event.TableModelEvent(
                this, ligne, ligne, javax.swing.event.TableModelEvent.ALL_COLUMNS,
                javax.swing.event.TableModelEvent.INSERT));
    }

    public void suppLignes(int[] lesLignes) {
        int nb = lesLignes.length;
        for (int i = 0; i < nb; i++) {
            suppLigne(lesLignes[i]);
        }
    }
    public void suppLigne(int ligne) {
        divorceRepo.deleteObject(donnees.get(ligne));
        donnees.remove(ligne);
        fireTableRowsDeleted(ligne, ligne);
    }
    public void suppAll() {
        int tmp = donnees.size()-1;
        donnees.clear();
        fireTableRowsDeleted(0, tmp);
    }
}
